<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="名称" style="width: 100%">
              <j-input placeholder="请输入名称" v-model="queryParam.title" style="width: 200px"></j-input>
            </a-form-item>
          </a-col>
<!--          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="包名" style="width: 100%">
              <j-input placeholder="请输入包名" v-model="queryParam.apk" style="width: 200px"></j-input>
            </a-form-item>
          </a-col>-->
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="" style="width: 100%">
              <a-space>
                <a-button type="primary" @click="searchQuery" style="margin-left: 20px;">查询</a-button>
                <a-button type="primary" @click="searchReset">查询</a-button>
              </a-space>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd(find.appId)" type="primary" icon="plus">新增</a-button>
      <!-- <a-button type="primary" icon="download" @click="handleExportXls('cms_app_version')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload> -->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete"/>
            删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作
          <a-icon type="down"/>
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a
        style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table ref="table" size="middle" :scroll="{x:true}" bordered rowKey="id" :columns="columns"
               :dataSource="dataSource" :pagination="ipagination"
               :loading="loading" :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
               class="j-table-force-nowrap"
               @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="55px" alt=""
               style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical"/>
          <a-dropdown>
            <a class="ant-dropdown-link">更多
              <a-icon type="down"/>
            </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <cms-app-version-modal ref="modalForm" @ok="modalFormOk"></cms-app-version-modal>
  </a-card>
</template>

<script>
import {httpAction, getAction} from '@/api/manage'
import '@/assets/less/TableExpand.less'
import {mixinDevice} from '@/utils/mixin'
import {JeecgListMixin} from '@/mixins/MyJeecgListMixin'
import CmsAppVersionModal from './modules/CmsAppVersionModal'

export default {
  name: 'CmsAppVersionList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    CmsAppVersionModal
  },
  data() {
    return {
      find: {
        appId: "",
        title: "",
        type: "",
        apk: "",
      },
      description: 'cms_app_version管理页面',
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '应用编号',
          align: "center",
          dataIndex: 'appId'
        },
        {
          title: '策略名称',
          align: "center",
          dataIndex: 'title'
        },
        {
          title: '升级说明',
          align: "center",
          dataIndex: 'remark'
        },
        {
          title: '版本号',
          align: "center",
          dataIndex: 'version'
        },
        {
          title: '升级方式',
          align: "center",
          dataIndex: 'mode_dictText'
        },
        {
          title: '指定源版本',
          align: "center",
          dataIndex: 'srcVersionId'
        },
        {
          title: '升级渠道',
          align: "center",
          dataIndex: 'channel_dictText'
        },
        {
          title: '链接',
          align: "center",
          dataIndex: 'url'
        },
        {
          title: '状态',
          align: "center",
          dataIndex: 'state_dictText'
        },
        {
          title: '是否定时',
          align: "center",
          dataIndex: 'isTiming_dictText'
        },
        {
          title: '定时启动时间',
          align: "center",
          dataIndex: 'startTime',
          customRender: function (text) {
            return !text ? "" : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '策略启动条件',
          align: "center",
          dataIndex: 'conditions_dictText'
        },
        {
          title: '创建时间',
          align: "center",
          dataIndex: 'createTime',
          customRender: function (text) {
            return !text ? "" : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '修改时间',
          align: "center",
          dataIndex: 'updateTime',
          customRender: function (text) {
            return !text ? "" : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },

        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          fixed: "right",
          width: 147,
          scopedSlots: {customRender: 'action'}
        }
      ],
      url: {
        list: "/common/cmsAppVersion/list",
        delete: "/common/cmsAppVersion/delete",
        deleteBatch: "/common/cmsAppVersion/deleteBatch",
        exportXlsUrl: "/common/cmsAppVersion/exportXls",
        importExcelUrl: "common/cmsAppVersion/importExcel",

      },
      dictOptions: {},
    }
  },
  created() {

    if (this.$route.query.appId != null) {
      this.find.appId = this.$route.query.appId;
      this.queryParam.appId = this.$route.query.appId;
      this.loadData();
      // console.log(this.find.appId);
      // this.getVersionList(this.$route.query.appId)
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    searchReset() {
      this.queryParam = {};
      if (this.$route.query.appId != null) {
        this.queryParam.appId = this.$route.query.appId;
      }
      this.loadData(1);
    },
    //根据传递的appid获取列表
    getVersionList(appId) {
      getAction(this.url.list, {appId: appId}).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records
          this.ipagination.total = res.result.total
          console.log(res.result.records)
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
    //条件查询
    queryClick() {
      this.find.appId = this.appid
      getAction(this.url.list, this.find).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records
          this.ipagination.total = res.result.total
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },

    myOK() {
      console.log(this.$route.params.appid)
      if (!this.url.list) {
        this.$message.error("请设置url.list属性!")
        return
      }

      this.loading = true
      getAction(this.url.list, {appId: this.$route.params.appid}).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records
          this.ipagination.total = res.result.total
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    }
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>